/**
 * @(#)com.framework.web.wechat.service.WeChatCartService
 *
 * Copyright (c) 2014-2018 一抹风光
 *
 * DESC:
 *
 */
package com.framework.web.wechat.service;

import com.framework.core.domain.SessionVo;
import com.framework.web.bus.domain.*;
import com.framework.web.bus.service.*;
import com.framework.web.common.domain.BusinessConstants;
import com.framework.web.wechat.domain.WeChatRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;

/**
 * @author fusf
 * @version 1.0  2016/9/17
 */
@Service
public class WeChatCartService {

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private GoodsInfoService goodsInfoService;

    @Autowired
    private OrderItemInfoService orderItemInfoService;

    @Transactional
    public String createOrder(WeChatRequest requestInfo, SessionVo sessionVo) {
        String userId = sessionVo.getUserId();
        String goodsIds = requestInfo.getGoodsIds();//商品IDS
        String addressId = requestInfo.getAddressId();//地址ID
        //根据商品地址，获取详细信息
        UserAddress userAddress = userAddressService.findById(addressId);
        //根据商品ID获取购物车中商品的数量
        Order order = new Order();
        order.setOrderDate(new Date());//下单时间
        order.setUserId(userId);
        order.setOrderStatus(BusinessConstants.ORDER_STATUS.NON_PAYMENT);
        order.setShippingAddress(userAddress.getCompleteAddress());
        order.setShippingName(userAddress.getConsigneeName());
        order.setShippingTel(userAddress.getConsigneeTel());
        order.setBuyerMsg(requestInfo.getCustomerMessage());//客户留言
        order = orderService.save(order);

        //商品详情
        String[] array_goodsId = goodsIds.split(",");
        double totalAmount = 0;
        for (String goodsId : array_goodsId) {
            //用户和商品ID查询购物车
            Cart cart = cartService.findByUserIdAndGoodsId(userId, goodsId);
            GoodsInfo goodsInfo = goodsInfoService.findById(goodsId);
            OrderItemInfo orderItemInfo = new OrderItemInfo();
            orderItemInfo.setGoodsId(goodsId);//商品ID
            orderItemInfo.setOrderId(order.getId());//订单ID
            orderItemInfo.setGoodsName(goodsInfo.getTitle());//商品名称
            orderItemInfo.setGoodsSmImg(goodsInfo.getImg());//商品图片 TODO
            orderItemInfo.setGoodsDesc(goodsInfo.getDescription());//商品描述
            orderItemInfo.setQuantities(cart.getCount());//购买数量
            orderItemInfo.setPrice(goodsInfo.getPrice());//原价
            orderItemInfo.setCurrPrice(goodsInfo.getCurrentPrice());//单价
            orderItemInfo.setTotal(goodsInfo.getCurrentPrice() * cart.getCount());//总价
            orderItemInfo.setStatus(BusinessConstants.STATUS.NORMAL);//有效
            orderItemInfo.setCreateTime(new Date());
            orderItemInfo.setUpdateTime(new Date());
            orderItemInfoService.save(orderItemInfo);
            totalAmount += orderItemInfo.getTotal();//总价
            //删除购物车
            cartService.delete(cart.getId());
        }

        order.setCashPayment(totalAmount);//现金支付金额
        order.setTotalAmount(totalAmount);
        order.setCouponPayment(0.0);//TODO
        String orderId = order.getId();
        return orderId;
    }
}
